package com.example.shirotest.utils;

import com.example.shirotest.entry.HtmlRegex;
import com.example.shirotest.entry.Result;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

public class HtmlParse {


    public static ArrayList<Result> getNetContent(String url, HtmlRegex htmlRegex) throws IOException {
        String regex = htmlRegex.getCell();  //单元内容

        URL urlobj = new URL(url);
        String hosturl = urlobj.getProtocol() + "://" + urlobj.getHost();
        String hostpro = urlobj.getProtocol();

        String charset = htmlRegex.getCharset();
        if(charset==null || charset==""){
            charset = "utf-8";
        }

        Document doc = HttpUtil.get(url,charset);
        if(doc==null)return null;

        Elements elements = doc.select(regex);

        if(elements.size()==0)return null;

        String get_title = "";
        String get_url   = "";
        String get_time  = "";
        String get_cover = "";
        String get_remark= "";

        ArrayList<Result> results = new ArrayList<>();
        for (Element element : elements) {
            Result result = new Result();
            //抓取标题
            get_title = parseElementByRegex(element,htmlRegex.getTitle_name(),htmlRegex.getTitle_value());
            if(get_title.equals(""))continue;

            //抓取地址
            get_url = parseElementByRegex(element,htmlRegex.getUrl_name(),htmlRegex.getUrl_value());
            get_url = parseHref(get_url,url,hostpro,hosturl);
            if(get_url.equals(""))continue;

            //抓取时间
            get_time = parseElementByRegex(element, htmlRegex.getTime_name(),htmlRegex.getTime_value());

            //抓取封面图片
            get_cover = parseElementByRegex(element,htmlRegex.getImg_name(),htmlRegex.getImg_value());
            get_cover = parseHref(get_cover,url,hostpro,hosturl);

            //抓取简介
            get_remark = parseElementByRegex(element,htmlRegex.getRemark_name(),htmlRegex.getRemark_value());

            result.setTitle(get_title);
            result.setUrl(get_url);
            result.setTimecreate(get_time);
            result.setImg(get_cover);
            result.setRemark(get_remark);

            results.add(result);
        }

        return results.size()==0?null:results;
    }

    //解析并获取元素
    public static String parseElementByRegex(Element element,String regex_name,String regex_value){
        if(regex_name==null || regex_name.equals(""))return "";
        if(regex_value==null || regex_value.equals(""))return "";

        String res = "";

        //保留字
        if(regex_value.equalsIgnoreCase("html")){
            if(regex_name.equalsIgnoreCase("this")){
                res = element.text();
            }else{
                res = element.select(regex_name).text();
            }
        } else {
            if(regex_name.equalsIgnoreCase("this")){
                res = element.attr(regex_value);
            }else {
                res = element.select(regex_name).attr(regex_value);
            }
        }

        return res;
    }

    //解析地址
    public static String parseHref(String href,String urllink,String hostpro,String hosturl){
        if(!href.matches("^http.*")){
            //相对当前连接的地址
            if(href.matches("^\\./.*")){
                href = urllink + href.substring(2);
            }

            //以//为开始的地址
            if(href.matches("^//.*")){
                href = hostpro + ":" + href;
            }

            //相对域名的地址
            if(href.matches("^/.*")){
                href = hosturl + href;
            }
        }

        return href;
    }


}
